package com.sfd.test.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.sfd.core.entity.ManyUser;
import com.sfd.core.module.Result;
import com.sfd.test.mapper.ManyUserMapper;
import com.sfd.test.service.ManyUserService;
import com.sfd.test.service.async.AsyncManyUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.Future;

/**
 * @author haden
 * @date 2022/12/2 17:52
 **/
@Service
@Slf4j
public class ManyUserServiceImpl extends ServiceImpl<ManyUserMapper, ManyUser> implements ManyUserService {
    @Resource
    private AsyncManyUserService asyncManyUserService;

    @Override
    public Result<String> getSize() {
        try {
            Integer pageSize = 1000;
            List<ManyUser> userList = Lists.newArrayList();
            Long startTime = System.currentTimeMillis();
            //1.普通查询上万条数据
            int i = 1;
            while (true) {
                Page<ManyUser> page = this.userPage(2000, 2022, 18, i, pageSize);
                if (CollectionUtil.isNotEmpty(page.getRecords())) {
                    userList.addAll(page.getRecords());
                } else {
                    break;
                }
                i++;
            }
            Long end = System.currentTimeMillis();
            //2.通过线程查询10000条数据
            List<ManyUser> userList2 = Lists.newArrayList();
            i = 1;
            Long startTime2 = System.currentTimeMillis();
            Page<ManyUser> userPage = userPage(2000, 2022, 18, i, pageSize);
            userList2.addAll(userPage.getRecords());
            List<Future<Page<ManyUser>>> futureList = Lists.newArrayList();
            for (i = 2; i <= userPage.getPages(); i++) {
                futureList.add(asyncManyUserService.asyncUserPage(2000, 2022, 18, i, pageSize));
            }
            if (CollectionUtil.isNotEmpty(futureList)) {
                for (Future<Page<ManyUser>> f : futureList) {
                    userList2.addAll(f.get().getRecords());
                }
            }
            Long end2 = System.currentTimeMillis();
            log.info("普通查询" + userList.size() + "条数据,耗时:" + (end - startTime) / 1000 + "毫秒");
            log.info("异步查询" + userList2.size() + "条数据,耗时:" + (end2 - startTime2) / 1000 + "毫秒");
        } catch (Exception e) {
            log.info(e.getMessage(), e);
        }
        return null;
    }

    @Override
    public Page<ManyUser> userPage(int startYears, int endYears, int age, Integer pageNo, Integer pageSize) {
        log.info(pageNo + "," + pageSize);
        Page<ManyUser> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(pageNo);
        LambdaQueryWrapper<ManyUser> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ManyUser::getAge, age);
        wrapper.between(ManyUser::getYear, startYears, endYears);
        wrapper.orderByDesc(ManyUser::getId);
        return this.page(page, wrapper);
    }
}
